import db from '../utils/dbconf'
import { SQL_ERROR } from '../server.config.js'
import numservice from './project-num'
const TABLE_NAME = 'work'


export default{
  add:async ({body:{work}},res,next) => {
		try{
			const { id:uid,type } = res.locals
			if(type !== 2) return res.send({code:500,msg:'用户状态异常'})
			const { proid,num,name,status } = work
			const tag = await db.table(TABLE_NAME)
					// .add(work)
					.thenAdd({ proid,num,status:Number(status),uid
            ,name:name?name:''
          },{uid,proid:work.proid},true)
					.then(({id,type})=>{
						if(type=='exist') return true
					})
      if(tag) return res.send({code:500,msg:'您已领取该订单'})
			await numservice.workadd({id:proid,plannum:num,status})
			return res.send({code:200})
		}catch(e){
			console.log({e})
			return res.send(SQL_ERROR)
		}
	},
  select:async (req,res,next) => {
    try{
      const { type } = res.locals
      if(![2,99].includes(type)) return res.send({code:500,msg:'用户状态异常'})
      let works
      if(type == 2){
        const { id:uid } = res.locals
        works = await db.table(TABLE_NAME)
          .where({uid,status:0})
          .order('createtime DESC')
          // .join({
            // table: 'table2',    join: 'inner',//left, right, inner三种方式    as: 'c' //表别名    on: ['id', 'id'] //ON 条件}).select();//SELECT * FROM `table` AS a LEFT JOIN `table2` AS b ON a.id = b.id LEFT JOIN `table3` AS c ON a.sid = c.sidmysql.table('table').alias('a').join({    table: 'table2',    join: 'left',    as: 'b'    on: ['id', 'id']}).join({    table: 'table3',    join: 'left',    as: 'c',    on: ['sid', 'sid']})
          .select()
      }else{
        const { uid } = req.query
        works = await db.table(TABLE_NAME)
          .where({uid})
          .order('createtime DESC')
          .select()
      }
      return res.send({code:200,data:{works}})
    }catch(e){
      console.log({e})
      return res.send(SQL_ERROR)
    }
  },
  delete:async ({body:{id,uid}},res,next) => {
    try{
      console.log({uid})
      const { id:u_id,type } = res.locals
      if(!type) return res.send({code:500,msg:'......'})
      if(type==2) uid = u_id
      console.log({type,uid})
      const item = await db.table(TABLE_NAME)
        .where({id,uid})
        .find()
      console.log({item})
      if(!item || !item.id) return res.send({code:500,msg:'已经删掉了'})
      await numservice.workcut(item)
      const num = await db.table(TABLE_NAME)
        .where({id,uid})
        .delete()
        .then(num=>num)
      console.log({num})
      return res.send(num==1?
        {code:200}:
        {code:500,msg:'程序异常'}
      )
    }catch(e){
      console.log({e})
      return res.send(SQL_ERROR)
    }
  },
  over:async ({body:{id,proid,num}},res,next) => {
    try{
      const { id:uid,type } = res.locals
      if(type !== 2) return res.send({code:500,msg:'用户状态异常'})
      await db.table(TABLE_NAME)
        .where({id,uid,proid})
        .update({status:1})
      await numservice.over({id:proid,num})
      return res.send({code:200,data:{}})
    }catch(e){
      console.log({e})
      return res.send(SQL_ERROR)
    }
  },
	update: async ({body:{work:{name,id}}},res,next) => {
		try{
		  const { id:uid,type } = res.locals
			// const { name } = work
		  if(type !== 2) return res.send({code:500,msg:'用户状态异常'})
		  await db.table(TABLE_NAME)
		    .where({id,uid})
		    .update({name})
		  // await numservice.over({id:proid,num})
		  return res.send({code:200,data:{}})
		}catch(e){
		  console.log({e})
		  return res.send(SQL_ERROR)
		}
	},
	stat:async ({query},res,next) => {
		try{
			const { id,type } = res.locals
			if(![2,99].includes(type)) return res.send({code:500,msg:'用户权限异常'})
			const works = await db.table(TABLE_NAME)
			  .join({
			    table:'project',
			    as:'pro',
			    join:'left',
			    on:['proid','id']
			  })
				.where(`${TABLE_NAME}.uid = ${id}`)
				// .field('allocation,other',true)
				.select()
			console.log({works})
			return res.send({code:200,data:{works}})
		}catch(e){
			console.log({e})
			return res.send(SQL_ERROR)
		}
	}
}